package com.xyazm.order.statemachine;

import com.xyazm.common.enums.OrderStatusChangeEnum;
import com.xyazm.common.enums.OrderStatusEnum;
import com.xyazm.order.enums.AfterSaleStateChangeEnum;
import com.xyazm.order.enums.AfterSaleStatusEnum;
import com.xyazm.order.statemachine.action.AfterSaleActionFactory;
import com.xyazm.order.statemachine.action.OrderActionFactory;
import org.springframework.stereotype.Component;
import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;

import javax.annotation.Resource;

/**
 * 状态机工厂
 * @author xuyou
 * @date 2023年01月15日 19:22
 */
@Component
public class StateMachineFactory {

     // 正向订单
     private final UntypedStateMachineBuilder orderStateMachineBuilder;
    @Resource
    private OrderActionFactory orderActionFactory;

    // 售后订单
    private final UntypedStateMachineBuilder afterSaleStateMachineBuilder;
    @Resource
    private AfterSaleActionFactory afterSaleActionFactory;

    // 初始化
    private StateMachineFactory() {
        // 基于自定义的订单状态机的class，构建了一个订单状态机的builder
        this.orderStateMachineBuilder = StateMachineBuilderFactory.create(OrderStateMachine.class);

        // 针对生成的builder构建状态流转的链条
        // OrderStatusChangeEnum 包含了订单状态流转所有的事件，每个事件都会导致订单状态流转
        for (OrderStatusChangeEnum event : OrderStatusChangeEnum.values()) {
            this.orderStateMachineBuilder.externalTransition()
                    .from(event.getFromStatus())
                    .to(event.getToStatus())
                    .on(event)
                    .callMethod("onStateChange"); // 状态流转触发之后,squirrel框架会回调自定义的方法去处理状态的流转
        }

        // 初始化和构建订单逆向状态流转的状态机
        this.afterSaleStateMachineBuilder = StateMachineBuilderFactory.create(AfterSaleStateMachine.class);
        for (AfterSaleStateChangeEnum event : AfterSaleStateChangeEnum.values()) {
            this.afterSaleStateMachineBuilder.externalTransition()
                    .from(event.getFromStatus())
                    .to(event.getToStatus())
                    .on(event)
                    .callMethod("onStateChange");
        }
    }

    /**
     * 获取订单状态机
     * @author xuyou
     * 2023-01-30 22:06
     * @param initState 初始化状态
    */
    public OrderStateMachine getOrderStateMachine(OrderStatusEnum initState) {
        OrderStateMachine orderStateMachine = this.orderStateMachineBuilder.newUntypedStateMachine(initState);
        orderStateMachine.setOrderActionFactory(orderActionFactory);
        return orderStateMachine;
    }

    /**
     * 获取售后状态机
     *
     * @param initState 初始状态
     * @return 状态机
     */
    public AfterSaleStateMachine getAfterSaleStateMachine(AfterSaleStatusEnum initState) {
        AfterSaleStateMachine afterSaleStateMachine = this.afterSaleStateMachineBuilder.newUntypedStateMachine(initState);
        afterSaleStateMachine.setAfterSaleActionFactory(afterSaleActionFactory);
        return afterSaleStateMachine;
    }

}
